import java.util.*;

public class Leetcode1475 {
    /**
     * 模拟
     *
     * @param prices
     * @return
     */
    public int[] finalPrices(int[] prices) {
        for (int i = 0; i < prices.length; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                if (prices[j] <= prices[i]) {
                    prices[i] -= prices[j];
                    break;
                }
            }
        }

        return prices;
    }

    /**
     * 单调栈
     *
     * @param prices
     * @return
     */
    public int[] finalPrices1(int[] prices) {
        Deque<Integer> deque = new ArrayDeque<>();
        for (int i = 0; i < prices.length; i++) {
            while (!deque.isEmpty() && prices[deque.peekLast()] >= prices[i]) {
                prices[deque.pollLast()] -= prices[i];
            }

            deque.addLast(i);
        }

        return prices;
    }
}
